<?php

/***
 * function:websocket 服务
 * created:2019-09-25
 * author:zhulei
 */
namespace App\Services;

use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
use DB;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use Illuminate\Support\Facades\Log;


class WebSocketService implements WebSocketHandlerInterface
{
    public function __construct()
    {

    }

    public function onOpen(Server $server, Request $request)
    {
        Log::info("server: handshake success with fd{$request->fd}\n");
        $server->push($request->fd, 'Welcome to LaravelS');
    }
    public function onMessage(Server $server, Frame $frame)
    {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, '我是:'.$frame->fd."   我发送了   ".$frame->data.'   当前日期是:'.date('Y-m-d H:i:s'));
    }
    public function onClose(Server $server, $fd, $reactorId)
    {
        Log::info('WebSocket 连接关闭');
        echo "client {$fd} closed\n";
    }


}
